﻿using Microsoft.Graphics.Canvas;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using 纸片人.AttributeUsageFloder;
using 纸片人.InfoDataFolder;

// https://go.microsoft.com/fwlink/?LinkId=234238 上介绍了“空白页”项模板

namespace 纸片人.PageViews
{
    [LastModified("2022-5-14", "createPencilStrech2.xaml")]
    [PageType("cpu", "素描效果2")]
    /// <summary>
    /// 可用于自身或导航至 Frame 内部的空白页。
    /// </summary>
    public sealed partial class PencilStrech2 : Page
    {
        LayerBitmapData bitmapData = new LayerBitmapData();
        public PencilStrech2()
        {
            this.InitializeComponent();
            Init();
        }

        private void Init()
        {
            selectPicture.Click += async (s, e) => { await bitmapData.SelectOneBitmapAsync(); canvas.Invalidate(); };
            canvas.Draw += (s, e) =>
            {
                Function.DrawGridGraphics(e.DrawingSession);

                if (bitmapData.oneBitmap != null)
                {
                    var element = (FrameworkElement)s;
                    float effectWidth = (float)element.ActualWidth;
                    float effectHeight = (float)element.ActualHeight * 0.7f;
                    float previewWidth = effectWidth;
                    float previewHeight = (float)element.ActualHeight * 0.3f;


                    var previewTran = Function.CalcutateImageCenteredTransform(previewWidth, previewHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    previewTran.Source = bitmapData.oneBitmap;
                    e.DrawingSession.DrawImage(previewTran, 0, effectHeight);

                    var resultColors = Black(bitmapData.oneColors, bitmapData.oneWidth, bitmapData.oneHeight, 255 - (int)edgeIntensity.Value);
                    var effectBitmap = CanvasBitmap.CreateFromColors(canvas, resultColors, bitmapData.oneWidth, bitmapData.oneHeight);
                    var effectTran = Function.CalcutateImageCenteredTransform(effectWidth, effectHeight, bitmapData.oneWidth, bitmapData.oneHeight);
                    effectTran.Source = effectBitmap;
                    e.DrawingSession.DrawImage(effectTran);
                }
            };
            edgeIntensity.ValueChanged += (s, e) =>
            {
                canvas.Invalidate();
            };
        }

        Color[] Black(Color[] colors, int width, int height, int edgeIntensity)
        {

            var shap = PictureFilter.PencilEffect(colors, width, height, edgeIntensity);
            var ca = new Color[shap.Length];
            var cache = new Color[shap.Length];
            int i = 0;
            foreach (var item in shap)
            {
                if (item.B < 240)
                {
                    cache[i] = Color.FromArgb(255, 0, 0, 0);
                }
                else
                {
                    cache[i] = Color.FromArgb(255, 255, 255, 255);
                }
                i++;
            }
            for (int x = 1; x < width - 2; x++)
            {
                int sy = 0, ey = 0;
                bool start = false;

                for (int y = 1; y < height - 2; y++)
                {
                    var item = cache[x + y * width];

                    if (item.A == 0)
                    {
                        item.B = 255;
                    }

                    var r = cache[x + 1 + y * width];
                    var b = cache[x + (y + 1) * width];

                    var rc = Math.Abs(item.B - r.B);
                    var bc = Math.Abs(item.B - b.B);

                    if (false)
                    {
                        if (rc > 10 || bc > 10)
                        {
                            ca[x + y * width] = Color.FromArgb(255, 0, 0, 0);
                        }
                        else
                        {
                            ca[x + y * width] = Color.FromArgb(255, 255, 255, 255);
                        }
                    }
                    else
                    {
                        if (bc > 10 || rc > 10)
                        {
                            if (start == false)
                            {
                                ca[x + y * width] = Color.FromArgb(255, 255, 255, 255);
                                sy = y;
                                start = true;
                            }
                            else
                            {
                                ca[x + y * width] = Color.FromArgb(255, 255, 255, 255);
                                ey = y;
                                int scy = sy + (ey - sy) / 2;
                                ca[x + scy * width] = Color.FromArgb(255, 0, 0, 0);
                                start = false;
                            }
                        }
                        else
                        {
                            ca[x + y * width] = Color.FromArgb(255, 255, 255, 255);
                        }
                    }
                }
            }
            return ca;
        }

    }
}
